//
//  ClubPropertyViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/20.
//

import UIKit

class ClubPropertyViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("我的资产").color(.black).font(16.font_semiBold).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubPropertyTableViewCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        return tableView
    }()
    
    private var model = ClubMinePropertyModel()
    private var propertyData: [[ClubSettingModel]] = [
        [.proGift],
        [.proIntegral, .proGoods]
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            backButton
            titleLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.Leading == backButton.Trailing
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(0)
        
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    private func loadData(){
        
        Task {
            let res = await network.request(.myAssets,type: ClubMinePropertyModel.self)
            if res.isSuccess, let model = res.model {
                self.model = model
                self.tableView.reloadData()
            }
        }
    }
}


extension ClubPropertyViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return propertyData.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubPropertyTableViewCell", for: indexPath) as! ClubPropertyTableViewCell
        cell.models = propertyData[indexPath.section]
        cell.contentMD = self.model
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 0 {
            return 62.0.Scale
        }else{
            return 112.0.Scale
        }
    }
}
